<template>
	<view class="">
		<view class="bg-f mt16 pl30 pb120">
			<u-form :model="form" ref="uForm" label-width="150" >
					<u-form-item label="收货人"><u-input v-model="form.name" placeholder="请输入收货人姓名"/></u-form-item>
					<u-form-item label="手机号码"><u-input v-model="form.phone" placeholder="请输入手机号码"/></u-form-item>
					<u-form-item label="所在地区">
						<u-input v-model="form.address" placeholder="请选择所在地区" @click='value=true' :disabled='true'/>
					</u-form-item>
					<u-form-item label="详细地址"><u-input v-model="addDetail" placeholder="请输入详细地址，如街道、门派号等"/></u-form-item>
					<view class="mt20 flex col-center">
						<radio color="#D23030" :checked="isDefault" @click="isDefault=!isDefault"></radio>
						<text>是否设置为默认地址</text>
					</view>
			</u-form>
		</view>
		<!-- 底部的确认按钮 -->
		<view class="footerbtn h88" @click="submit">
			确认
		</view>
		<city-select v-model="value" @city-change="cityChange"></city-select>
	</view>
</template>

<script>
	import citySelect from '@/components/citySelect/u-city-select.vue'
	export default {
		name:"editAdd",
		components:{citySelect},
		onLoad(opt) {
			console.log(opt)
			this.id=opt.id
			this.from=opt.from
			if(opt.from==1) {
				this.getAddDetail()
			}
		},
		data() {
			return {
				//地址id
				id:'',
				// 选择地区
				value:false,
				//from=1编辑地址  from=2新增地址
				from:'',
				// add:'',
				addDetail:'',
				isDefault:false,
				form:{
					name:'',
					phone:'',
					address:'',
					addr:'',
					is_default:0
				},
			};
		},
		methods:{
			//获取地址详情
			getAddDetail(){
				this.$api.address_details({id:this.id}).then(res=>{
					console.log(res)
					this.form=res.data
					this.addDetail=res.data.addr
				})
			},
			submit() {
				this.form.addr=this.addDetail
				this.isDefault ? this.form.is_default=1 : this.form.is_default=0
				let api=''
				if(this.from==1) {
					//如果等于1   调用编辑地址的接口
					api='editAdd' 
				}else if(this.from==2) {
					//如果等于2   调用新增地址的接口
					api='addAddress'
				}
				this.$api[api](this.form).then(res=>{
					// console.log(res)
					this.$msg(res.msg)
					setTimeout(()=>{
						uni.navigateBack()
					},800)
				})
			},
			cityChange(e) {
				this.form.address = e.province.label + '-' + e.city.label + '-' + e.area.label;
			}
		}
	}
</script>

<style lang="scss">
.footerbtn {
		position: fixed;
		width: 90%;
		left: 50%;
		bottom: 60rpx;
		transform: translateX(-50%);
		background: #D23030;
		box-shadow: 0px 2rpx 10rpx 0px rgba(210,48,48,1);
		border-radius: 44rpx;
		font-size: 28rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 88rpx;
	}
</style>
